import java.util.*;

class LocalEnv implements Env {

    Map<String, Type> local =  new HashMap<String, Type>();
    
    Env globalEnv;

    Type result;

    LocalEnv(Env _globalEnv) {
	globalEnv = _globalEnv;
    }
    
    public void insert(String s, Type t, Position p) {
	System.out.println("Locally inserting "+s);
	local.put(s,t);
    }

    public void insert(String s, Type t) throws SemanticException{
	if(local.get(s) != null)
	    throw new SemanticException("Identifier " + s +" doubly defined");
	    
	local.put(s,t);
    }

    public Type lookup(String s) {
	//System.out.println("Local lookup: "+s);
	Type r = local.get(s);
	if (r!=null) return r;
	else return globalEnv.lookup(s);
    }

    public void setResult(Type t) {
	result = t;
    }

    public Type getResult() {
	return result;
    }

    public Env enter() {
	throw new IllegalStateException("Already in local environment");
    }

    public void print()
    {
	int nrEntries = local.size();
	Set entries = local.entrySet();

	Iterator i = entries.iterator();
	
 	System.out.println("*****BEGIN LOCAL TABLE **********");
	while(i.hasNext())
	{
	    Map.Entry entry = (Map.Entry)i.next(); 
	    System.out.println("[" + entry.getKey() + "][" + entry.getValue().toString() + "]");
	}		
	System.out.println("*******END LOCAL TABLE **********");
    }
}
